using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MonoCash
{
    static class Program
    {
        public static Settings AppSettings = new Settings();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                init();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error loading objects!\n" + ex.Message);
            }
            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new AppWindow());
            }
            else
            {
                Console.WriteLine("MonoCash started in command line mode");
                foreach (string arg in args)
                {
                    try
                    {
                        switch (arg)
                        {
                            case "--GetPrices":
                                Console.WriteLine("Downloading Prices");
                                int result = AppSettings.Data.GetPrices();
                                Console.WriteLine(result.ToString() + " prices downloaded.");
                                break;
                            case "--LoadDefaults":
                                Console.WriteLine("Installing default chart of accounts");
                                Program.AppSettings.InstallDefaults();
                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error occurred!\n" + ex.Message);
                    }
                }
                
            }
        }
        static void init()
        {
            //Load Backend
            try
            {
                AppSettings.Data = (MonoCash.Data.DataBackend)Activator.CreateInstance(Type.GetType(AppSettings.DataStorageType));
                AppSettings.Data.LoadData();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error loading data backend. Check settings.\n" + ex.Message);
            }

            //Load price engine
            try
            {
                AppSettings.PriceEngine = (MonoCash.Data.QuoteEngine)Activator.CreateInstance(Type.GetType(AppSettings.PriceEngineType));
            }
            catch
            {
                Console.WriteLine("Error loading data backend. Check settings.");
            }
        }
    }
}